# -*- coding: utf-8 -*-
from typing import TypeVar, Generic, Optional
from pydantic import BaseModel
from fastapi import status
from fastapi.responses import JSONResponse

T = TypeVar('T')


class R(BaseModel, Generic[T]):
	code: int
	data: Optional[T] = None
	message: Optional[str]

	@classmethod
	def error(cls, message: str) -> JSONResponse:
		r = cls(code=400, message=message)
		return JSONResponse(status_code=status.HTTP_200_OK, content=r.dict())

	@classmethod
	def success(cls, data: T, message: str) -> JSONResponse:
		r = cls(code=200, data=data, message=message)
		return JSONResponse(status_code=status.HTTP_200_OK, content=r.dict())
